home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / BARMDI.PAK / STATBAR.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  346 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: statbar.c
  9. //
  10. //  PURPOSE: Handles general routines for the Barsdi sample.
  11. //
  12. //  FUNCTIONS:
  13. //    MsgTimer      - Handles the WM_TIMER messages to set the time on
  14. //                    the status bar.
  15. //    MsgMenuSelect - Handle the WM_MENUSELECT message. This message will
  16. //                    enable the status bar control to update when the user
  17. //                    moves across menu items on the main window.
  18. //    InitializeStatusBar - Initialize statusbar control with time and
  19. //                    mouse positions.
  20. //    CreateSBar    - Calls CreateStatusWindow() to create the status bar
  21. //    UpdateStatusBar - Updates the statusbar control with appropriate text
  22. //
  23.  
  24. #include <windows.h>            // required for all Windows applications
  25. #include <windowsx.h>
  26. #include <commctrl.h>           // prototypes and defs for common controls
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "statbar.h"            // prototypes specific to statbar.c
  29. #include "resource.h"
  30.  
  31.  
  32. // Global Variable for the status bar control.
  33.  
  34. HWND  hWndStatusbar;
  35.  
  36. //  **TODO**  Add entries to the string table in barsdi.rc for each menu
  37. //            command.  MsgMenuSelect (below) loads these strings to display
  38. //            information in the status bar.  MsgMenuSelect assumes that the
  39. //            string ID is the same as the command ID and that a string
  40. //            exists for every command.
  41. //
  42. // The following array contains resource string ID's for popup menus
  43. // in the main application menu.  This array is used by MsgMenuSelect
  44. // to display information in the status bar.
  45. //
  46. //  **TODO**  Add entries to this array for each popup menu in the same
  47. //            positions as they appear in the main menu.  Remember to define
  48. //            the ID's in globals.h and add the strings to barsdi.rc.
  49.  
  50. UINT idPopup[] =
  51. {
  52.     IDS_MDISYSMENU,     // **BARMDI** Maximized MDI child system menu
  53.     IDS_FILEMENU,
  54.     IDS_EDITMENU,
  55.     IDS_WINDOWMENU,
  56.     IDS_HELPMENU,
  57. };
  58.  
  59.  
  60. //
  61. //  FUNCTION: MsgTimer(HWND, UINT, WPARAM, LPARAM)
  62. //
  63. //  PURPOSE: Calls GetLocalTime() to set the time on the status bar
  64. //
  65. //
  66. //  PARAMETERS:
  67. //
  68. //    hwnd      - Window handle  (Unused)
  69. //    uMessage  - Message number (Unused)
  70. //    wparam    - Extra data     (Unused)
  71. //    lparam    - Extra data     (Unused)
  72. //
  73. //  RETURN VALUE:
  74. //
  75. //    Always returns 0 - Message handled
  76. //
  77. //  COMMENTS:
  78. //
  79. //    Every time the window procedure receives a Timer message, it calls
  80. //    GetLocalTime() to obtain the time and then formats the time into
  81. //    a string. The time sting is then displayed on the status bar.
  82. //
  83.  
  84. #pragma argsused
  85. LRESULT MsgTimer(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  86. {
  87.     char        szBuf[16];      // Temp buffer.
  88.     SYSTEMTIME  sysTime;
  89.  
  90.     GetLocalTime(&sysTime);
  91.  
  92.     wsprintf(szBuf,
  93.              "%2d:%02d:%02d %s",
  94.              (sysTime.wHour == 0 ? 12 :
  95.              (sysTime.wHour <= 12 ? sysTime.wHour : sysTime.wHour -12)),
  96.              sysTime.wMinute,
  97.              sysTime.wSecond,
  98.              (sysTime.wHour < 12 ? "AM":"PM"));
  99.  
  100.     UpdateStatusBar(szBuf, 2, 0);
  101.      return 0;
  102. }
  103.  
  104.  
  105. //
  106. //  FUNCTION: MsgMenuSelect(HWND, UINT, WPARAM, LPARAM)
  107. //
  108. //  PURPOSE:  Upadates menu selections on the staus bar.
  109. //
  110. //
  111. //  PARAMETERS:
  112. //
  113. //    hwnd      - Window handle  (Used)
  114. //    uMessage  - Message number (Used)
  115. //    wparam    - Extra data     (Used)
  116. //    lparam    - Extra data     (Used)
  117. //
  118. //  RETURN VALUE:
  119. //
  120. //    Always returns 0 - Message handled
  121. //
  122. //  COMMENTS:
  123. //    This message is sent when the user selects menu items by
  124. //    by pulling down  a popup menu move the mouse around to highlite
  125. //    different menu items.
  126. //
  127. //
  128.  
  129. #pragma argsused
  130. LRESULT MsgMenuSelect(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  131. {
  132.     static char szBuffer[128];
  133.      char   szTitle[32];
  134.      LPSTR  lpTitle;
  135.      UINT   nStringID;
  136.      UINT   fuFlags = GET_WM_MENUSELECT_FLAGS(wparam, lparam) & 0xffff;
  137.     UINT   uCmd    = GET_WM_MENUSELECT_CMD(wparam, lparam);
  138.     HMENU  hMenu   = GET_WM_MENUSELECT_HMENU(wparam, lparam);
  139.  
  140.  
  141.     szBuffer[0] = 0;                            // First reset the buffer
  142.  
  143.  
  144.     if (fuFlags == 0xffff && hMenu == NULL)     // Menu has been closed
  145.         nStringID = IDS_DESCRIPTION;
  146.  
  147.     else if (fuFlags & MFT_SEPARATOR)           // Ignore separators
  148.         nStringID = 0;
  149.  
  150.     else if (fuFlags & MF_POPUP)                // Popup menu
  151.     {
  152.         if (fuFlags & MF_SYSMENU)               // System menu
  153.             nStringID = IDS_SYSMENU;
  154.  
  155.         else
  156.         {
  157.             // ***BARMDI*** If there is a maximized MDI child window,
  158.             //              its system menu will be added to the main
  159.             //              window's menu bar.  Since the string ID for
  160.             //              the MDI child's sysmenu is already in the
  161.             //              idPopup array, all we need to do is patch up
  162.             //              the popup menu index (uCmd) when the child's
  163.             //              system menu is NOT present.
  164.  
  165.             HWND hwndChild = (HWND)SendMessage(hwndMDIClient, WM_MDIGETACTIVE, 0, 0L);
  166.  
  167.             if (!hwndChild || !IsZoomed(hwndChild)) // No MDI child sysmenu
  168.                 uCmd++;
  169.  
  170.             // Get string ID for popup menu from idPopup array.
  171.             nStringID = ((uCmd < sizeof(idPopup)/sizeof(idPopup[0])) ?
  172.                             idPopup[uCmd] : 0);
  173.         }
  174.     }  // for MF_POPUP
  175.  
  176.     else                                        // Must be a command item
  177.     {
  178.         // ***BARMDI*** The Window menu has a dynamic part at the bottom
  179.         //              where the MDI Client window adds entries for each
  180.         //              child window that is open.  By getting the menu
  181.         //              item string we can customize the status bar string
  182.         //              with the name of the document.
  183.  
  184.           if (uCmd >= IDM_WINDOWCHILD && uCmd < IDS_HELPMENU)
  185.         {
  186.             LoadString(hInst, IDM_WINDOWCHILD, szBuffer, sizeof(szBuffer));
  187.  
  188.             GetMenuString(hMenu,
  189.                           uCmd,
  190.                           szTitle,
  191.                           sizeof(szTitle),
  192.                           MF_BYCOMMAND);
  193.  
  194.             lpTitle = szTitle;
  195.  
  196.             while (*lpTitle && *lpTitle != ' ')
  197.                 lpTitle++;
  198.  
  199.                 lstrcat(szBuffer, lpTitle);
  200.  
  201.             nStringID = 0;
  202.         }
  203.         else
  204.             nStringID = uCmd;               // String ID == Command ID
  205.     }
  206.  
  207.     // Load the string if we have an ID
  208.     if (0 != nStringID)
  209.         LoadString(hInst, nStringID, szBuffer, sizeof(szBuffer));
  210.  
  211.     // Finally... send the string to the status bar
  212.     UpdateStatusBar(szBuffer, 0, 0);
  213.  
  214.      return 0;
  215. }
  216.  
  217.  
  218. //
  219. //  FUNCTION: InitializeStatusBar(HWND)
  220. //
  221. //  PURPOSE:  Initialize statusbar control with time and mouse positions.
  222. //
  223. //
  224. //  PARAMETERS:
  225. //
  226. //  hwndParent - Window handle of the status bar's parent
  227. //
  228. //
  229. //  RETURN VALUE:  NONE
  230. //
  231. //
  232. //  COMMENTS:
  233. //
  234. //   This function initializes the time  and mouse positions sections of
  235. //   the statubar window. The Date for the time section is obtained by
  236. //   calling SetTimer API. When the timer messages start comming in,
  237. //   GetSytemTime() to fill the time section.
  238. //   The WPARAM of SB_SETTEXT is divided into 2 parameters. The LOWORD
  239. //   determines which section/part the text goes into, and the HIWORD
  240. //   tells how the bar is drawn (popin or popout).
  241. //
  242.  
  243. void InitializeStatusBar(HWND hwndParent)
  244. {
  245.     const cSpaceInBetween = 8;
  246.     int   ptArray[3];   // Array defining the number of parts/sections
  247.     SIZE  size;         // the Status bar will display.
  248.     RECT  rect;
  249.     HDC   hDC;
  250.  
  251.    /*
  252.     * Fill in the ptArray...
  253.     */
  254.  
  255.     hDC = GetDC(hwndParent);
  256.     GetClientRect(hwndParent, &rect);
  257.  
  258.     ptArray[2] = rect.right;
  259.  
  260.     if (GetTextExtentPoint(hDC, "00:00:00 PM", 12, &size))
  261.         ptArray[1] = ptArray[2] - (size.cx) - cSpaceInBetween;
  262.     else
  263.         ptArray[1] = 0;
  264.  
  265.     if (GetTextExtentPoint(hDC, "Time:", 6, &size))
  266.         ptArray[0] = ptArray[1] - (size.cx) - cSpaceInBetween;
  267.     else
  268.         ptArray[0] = 0;
  269.  
  270.     ReleaseDC(hwndParent, hDC);
  271.  
  272.     SendMessage(hWndStatusbar,
  273.                 SB_SETPARTS,
  274.                 sizeof(ptArray)/sizeof(ptArray[0]),
  275.                 (LPARAM)(LPINT)ptArray);
  276.  
  277.     UpdateStatusBar(SZDESCRIPTION, 0, 0);
  278.     UpdateStatusBar("Time:", 1, SBT_POPOUT);
  279. }
  280.  
  281.  
  282. //
  283. //  FUNCTION: CreateSBar(HWND, UINT, WPARAM, LPARAM)
  284. //
  285. //  PURPOSE:  Calls CreateStatusWindow() to create the status bar
  286. //
  287. //
  288. //  PARAMETERS:
  289. //
  290. //  hwndParent - Window handle of the status bar's parent
  291. //
  292. //  RETURN VALUE:
  293. //
  294. //  If both controls were created successfully Return TRUE,
  295. //  else returns FALSE.
  296. //
  297. //  COMMENTS:
  298. //
  299. //
  300.  
  301. BOOL CreateSBar(HWND hwndParent)
  302. {
  303.     hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_BORDER,
  304.                                        SZDESCRIPTION,
  305.                                        hwndParent,
  306.                                        IDM_STATUSBAR);
  307.     if(hWndStatusbar)
  308.     {
  309.         InitializeStatusBar(hwndParent);
  310.         SetTimer(hwndParent, IDM_TIMER, TIMER_TIMEOUT, NULL);
  311.         return TRUE;
  312.     }
  313.  
  314.     return FALSE;
  315. }
  316.  
  317.  
  318. //
  319. //  FUNCTION: UpdateStatusBar(HWND)
  320. //
  321. //  PURPOSE:  Updates the statusbar control with appropriate text
  322. //
  323. //
  324. //  PARAMETERS:
  325. //
  326. //  lpszStatusString - text to be displayed
  327. //  partNumber       - which part of the status bar to display text in
  328. //  displayFlags     - display flags
  329. //
  330. //
  331. //  RETURN VALUE: NONE
  332. //
  333. //
  334. //  COMMENTS:
  335. //      None
  336. //
  337. //
  338.  
  339. void UpdateStatusBar(LPSTR lpszStatusString, WORD partNumber, WORD displayFlags)
  340. {
  341.     SendMessage(hWndStatusbar,
  342.                 SB_SETTEXT,
  343.                 partNumber | displayFlags,
  344.                 (LPARAM)lpszStatusString);
  345. }
  346.